<?php
/************************************************************************
* Author:        Dennis T. Kaplan                                       *
* Web:           www.pzzazz.com                                         *
* Date Created:  01/27/2007                                             *
* Last Modified: 01/27/2007                                             *
* Version:       1.0                                                    *
* License:       GNU General Public License                             *
*************************************************************************
*                                                                       *
* Copyright (c) 2007 Dennis T. Kaplan                                   *
* All rights reserved.                                                  *
*                                                                       *
* This script is free software; you can use it under the terms of the   *
* GNU General Public License as published by the Free Software          *
* Foundation;                                                           *
*                                                                       *
* The GNU General Public License can be found at                        *
* http://www.gnu.org                                                    *
*                                                                       *
* This script is distributed in the hope that it will be useful,        *
* but WITHOUT ANY WARRANTY; without even the implied warranty of        *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the          *
* GNU General Public License for more details.                          *
*                                                                       *
*************************************************************************
* Class Messages used for sending errors and messages to the user       *
************************************************************************/
/*

Usage:
		setError('Some error decription');
		setMsg('Some error decription');
		
		Return: 
			$msg = getMsg();
		
			$msg->html = Includes the html
			$msg->time = Time to display
			$msg->countMsg = How many messages
			$msg->countWords = Total count of words
*/


class Msg
{
	var $msgList;
	var $time;
	var $html;
	var $countMsg;
	var $countWords;
	
	function Msg()
	{
		log_message('debug', 'Msg: initialized');
		$this->tpl['Messanger'] = 'No New Messages';
		//$this->resetMsgList();
	}
	
	function setError($err)
   {
		$this->msgList['set'] = TRUE;
		$this->msgList['err'][] = $err;
		log_message('debug', 'Messages: Error set: ' . $err );
		//$this->logError($err);
   }
	
	function setMsg($msg)
   {
		$this->msgList['set'] = TRUE;
		$this->msgList['ok'][] = $msg;
		log_message('debug', 'Messages: Message set: ' . $msg );
   }
	
	function resetMsgList()
	{
		$this->msgList = FALSE;
		$this->msgList = array();
		$this->msgList['set'] = FALSE;
		log_message('debug', 'Messages: Message List has been Reset');
	}

	function htmlMsg()
	{
		log_message('debug', 'Messages: htmlMsg are being checked' );
		if($this->msgList['set'] === TRUE){
			log_message('debug', 'htmlMsg found' );
			$this->countMsg = 1;
			$this->countWords = 0;
			$html = '';
			if(isset($this->msgList['err'])){
				log_message('debug', 'Messages: html Errors are being generated' );
				foreach($this->msgList['err'] as $k => $a){
					$html .= "<span style='color: Red;'>$a</span><br />";
					$this->countMsg = $this->countMsg + 1;
					$this->countWords = $this->countWords + str_word_count($a);
				}
			} 
			if(isset($this->msgList['ok'])){
				log_message('debug', 'Messages: html Message(s) are being generated' );
				foreach($this->msgList['ok'] as $k => $a){
					$html .= "<span style='color: Green;'>$a</span><br />";
					$this->countMsg = $this->countMsg + 1;
					$this->countWords = $this->countWords + str_word_count($a);
				}
			}
			$this->html = $html;
		}
	}	

	function jsMsg()
	{
		$js = "document.getElementById('MessageDiv').style.display='inline';\n";
		$js .= "setTimeout(\"document.getElementById('MessageDiv').style.display='none'\",$this->time);\n";
		$this->js = $js;
	}
	
	function getMsg()
	{
		log_message('debug', 'Messages: getMsg called' );
		$CI =& get_instance();
		if(isset($CI->validation->error_string)){
			if($CI->validation->error_string != ''){
				$this->setError($CI->validation->error_string);
				log_message('debug', 'Messages: Set to: ' . $this->msgList['set']);
			}
		}
		if($this->msgList['set'] === TRUE)
		{
			log_message('debug', 'Messages: Sending getMsg' );
			$this->htmlMsg();
			$this->timeMsg();
			$this->jsMsg();
			$msg = '';
			$msg->jsOnLoad = $this->js;
			$msg->Messanger = $this->html;
			return $msg;
		}
		else
		{
			return FALSE;
		}
	}	

	function timeMsg()
	{	
		$CI =& get_instance();
		log_message('debug', 'Messages: time calculation entered' );

		if($CI->session->userdata('messangertime') > 0)
		{
			$t = $CI->session->userdata('messangertime');
		}
		else 
		{
			$t = '1';
		}
		$this->time = (
			($this->countMsg * 2000) 
			+ 
			($this->countWords * 250)
			* 
			$t
		);
	}

	function logError($errstr,&$var = false)
	{
	   $errorstring = "<h2>" .date('Y-m-d H:i:s') ."</h2>\n";
      $errorstring .= "<p>Fatal Error: $errstr (# $errno).</p>\n";
      $errorstring .= "<p>Script: '{$_SERVER['PHP_SELF']}'.</p>\n";
	   $errorstring .= "<hr /><div>".$errstr."<br /><table border='1'>";
	   if ( $EZSQL_ERROR ) {
      	$errorstring .= $dbuser->vardump($EZSQL_ERROR);
		}
		if(isset($var))
			{
				print_r($var);
	   	}

	   $sOut=""; $aCallstack=debug_backtrace();
  		$errorstring .= "<thead><tr><th>file</th><th>line</th><th>function</th></tr></thead>";
	   foreach($aCallstack as $aCall)
   		{
   	    if (!isset($aCall['file'])) $aCall['file'] = '[PHP Kernel]';
   	    if (!isset($aCall['line'])) $aCall['line'] = '';

      $errorstring .= "<tr><td>{$aCall['file']}</td><td>{$aCall['line']}</td>
                           <td>{$aCall['function']}</td></tr>";
   }
   	$errorstring .=  "</table></div><hr /></p>";

		error_log($errorstring, 3, '/var/www/log/errorlog.html' ); //ERROR_LOG
	}
}
?>
